package com.design_pattern.visitor;

interface ComputerVisitor {
    void visitCPU(CPU cpu);

    void visitMemory(Memory memory);
}

/**
 * Visitor模式  适合于结构固定的类，  由用户实现访问各个组件的具体逻辑
 */
public class Computer {
    private ComputerPart cpu = new CPU();
    private ComputerPart memory = new Memory();

    public static void main(String[] args) {
        new Computer().accept(new VipComputerVisitor());
    }

    public void accept(ComputerVisitor visitor) {
        cpu.accept(visitor);
        memory.accept(visitor);
    }

}

abstract class ComputerPart {
    abstract void accept(ComputerVisitor visitor);

    abstract double getPrice();
}

class VipComputerVisitor implements ComputerVisitor {

    @Override
    public void visitCPU(CPU cpu) {
        System.out.println("visit cpu" + cpu.getPrice());

    }

    @Override
    public void visitMemory(Memory memory) {
        System.out.println("visit memory" + memory.getPrice());
    }
}

class CPU extends ComputerPart {

    @Override
    void accept(ComputerVisitor visitor) {
        visitor.visitCPU(this);
    }

    @Override
    double getPrice() {
        return 100;
    }
}

class Memory extends ComputerPart {


    @Override
    void accept(ComputerVisitor visitor) {
        visitor.visitMemory(this);
    }

    @Override
    double getPrice() {
        return 50;
    }
}





